(display "\n========================================\n")
(define (make-point x y)
    (cons x y))

(define (x-point p)
    (car p))

(define (y-point p)
    (cdr p))

(define (print-point p)
    (display "(")
    (display (x-point p))
    (display ",")
    (display (y-point p))
    (display ")\n"))

; (print-point (make-point 1 3))

(define (make-segment A B)
    (cons A B))

(define (start-segment AB)
    (car AB))

(define (end-segment AB)
    (cdr AB))

(define (length-of-segment AB)
    (let ((A (start-segment AB))
          (B (end-segment AB)))
         (sqrt (+ (square (- (x-point A) (x-point B)))
                  (square (- (y-point A) (y-point B)))))))

; (display (length-of-segment (make-segment (make-point 0 0)
;                                           (make-point 3 4))))
; (newline)

; =============================================================
; 构造方法一：利用对角点构造一个矩形
(define (make-rectangle A C)
    (let ((B (make-point (x-point C) (y-point A)))
          (D (make-point (x-point A) (y-point C))))
         (let ((AB (make-segment A B))
               (DC (make-segment D C)))
              (cons AB DC))))

(define (print-rectangle rectangle)
    (print-point (car (car rectangle)))
    (print-point (cdr (car rectangle)))
    (print-point (car (cdr rectangle)))
    (print-point (cdr (cdr rectangle))))

(define rect (make-rectangle (make-point 0 5) (make-point 5 0)))

(print-rectangle rect)

; 直接计算周长，不足之处在于只能计算“正规” 4 个点表示的矩形
(define (perimeter rectangle)
    (let ((w (- (x-point (car (car rectangle)))
                (x-point (cdr (car rectangle)))))
          (h (- (y-point (cdr (car rectangle)))
                (y-point (car (cdr rectangle))))))
         (* (+ (abs w) (abs h)) 2)))

; 直接计算面积，不足之处在于只能计算“正规” 4 个点表示的矩形
(define (area rectangle)
     (let ((w (- (x-point (car (car rectangle)))
                (x-point (cdr (car rectangle)))))
          (h (- (y-point (cdr (car rectangle)))
                (y-point (car (cdr rectangle))))))
         (abs (* w h))))

(display (perimeter rect))
(newline)
(display (area rect))
(newline)

; 计算以点构造的矩形的长
(define (length-of-rectangle r)
    (length-of-segment (make-segment (car (car r))
                                     (cdr (car r)))))

; 计算以点构造的矩形的宽
(define (width-of-rectangle r)
    (length-of-segment (make-segment (cdr (car r))
                                     (cdr (cdr r)))))

; 通用周长计算
(define (perimeter rectangle)
    (let ((length (length-of-rectangle rectangle))
          (width (width-of-rectangle rectangle)))
         (* (+ length width) 2)))

; 通用面积计算
(define (area rectangle)
    (let ((length (length-of-rectangle rectangle))
          (width (width-of-rectangle rectangle)))
         (* length width)))

(display (perimeter rect))
(newline)
(display (area rect))
(newline)

; =============================================================
; 构造方法二：利用相邻边构造矩形
(define (make-rectangle AB BC)
    (let ((A (start-segment AB))
          (B (end-segment AB))
          (C (end-segment BC)))
         (define D (make-point (x-point A) (y-point C)))
         (cons (cons (make-segment A B) (make-segment B C))
               (cons (make-segment C D) (make-segment D A)))))

(define rect (make-rectangle (make-segment (make-point 0 5) (make-point 5 5))
                         (make-segment (make-point 5 5) (make-point 5 0))))

(define (length-of-rectangle r)
    (length-of-segment (car (car r))))

(define (width-of-rectangle r)
    (length-of-segment (cdr (car r))))

(display (perimeter rect))
(newline)
(display (area rect))
(newline)

(display "\n========================================\n")
